import { assignPageFields, assignContentPageFields } from '@/utils/helpers'
import { allContentTypes, isMultiType, flatPrototypes, isAnnexByType } from '@/constants/structure'
import { leftItems, rightItems } from '@/constants/sidebar'

const getters = {
  sidebar: state => state.app.sidebar,
  size: state => state.app.size,
  device: state => state.app.device,
  dict: state => state.dict.dict,
  visitedViews: state => state.tagsView.visitedViews,
  cachedViews: state => state.tagsView.cachedViews,
  token: state => state.user.token,
  avatar: state => state.user.avatar,
  name: state => state.user.name,
  introduction: state => state.user.introduction,
  roles: state => state.user.roles,
  permissions: state => state.user.permissions,
  permission_routes: state => state.permission.routes,
  topbarRouters:state => state.permission.topbarRouters,
  defaultRoutes:state => state.permission.defaultRoutes,
  sidebarRouters:state => state.permission.sidebarRouters,
// editor
  addableTypes(state) {
    // 获取元素的 pageType 或 dataType(pageType=content时)
    const getType = (i) => {
      // 获取元素的 pageType 或 dataType(pageType=content时)
      if (i.pageType === 'content') {
        return i.blockList?.[0]?.dataType
      }
      return i.dataType || i.pageType
    }
    // 获取页面已有类型
    const usedTypes = state.xmleditor.editorPageList?.map(getType) || []
    // 可添加逻辑
    return flatPrototypes.filter((i) => {
      if (i.pageType === 'indexEn' || i.pageType === 'index') {
        // 1. 不能手动添加中文索引和英文索引
        // 2. 如果是文档且只能是术语文档，只有添加了术语和定义章节，自动生成索引章节，见 addPage.js
        // 3. 如果是文档且只能是术语文档，术语和定义章节删除，则中文索引和英文索引都会删除，见 removePage.js
        return false
      }
      // 1. 可添加多个的，如 chapter, normativeAnnex, informativeAnnex
      // 2. 不是禁止添加的，如 cover, scope
      // 3. 未添加的
      return isMultiType(i.key) || !usedTypes.includes(i.pageType || i.dataType)
    })
  },
  contentPages(state) {
    return state.xmleditor.editorPageList.filter((item) => item.pageType === 'content').map(assignContentPageFields)
  },
  annexPages(state) {
    return state.xmleditor.editorPageList.filter((item) => isAnnexByType(item.pageType)).map(assignPageFields)
  },
  indexNodes(state) {
    return state.xmleditor.indexNodes
  },
  ruleStructures(state) {
    return state.xmleditor.ruleStructures?.map((item) => {
      if (item.pageType === 'content') {
        item.sectionType = item.sectionType || 'chapter'
      } else if (item.pageType === 'annex') {
        item.sectionType = item.sectionType === 'normative' ? 'normativeAnnex' : 'informativeAnnex'
      } else {
        item.sectionType = item.pageType
      }
      item.required = item.required === '1'
      return item
    })
  },
  ruleExpressions(state) {
    const ruleMap = {}
    const pageTypeMap = {}
    const contentTypeMap = {}
    state.xmleditor.ruleStructures.forEach((item) => {
      // 原通过ruleStructures直接读取sectionType出现异常
      let pageType = item.pageType
      if (item.pageType === 'content') {
        pageType = item.sectionType || 'chapter'
      } else if (item.pageType === 'annex') {
        if (item.sectionType === 'normative' || item.sectionType === 'normativeAnnex') {
          pageType = 'normativeAnnex'
        } else {
          pageType = 'informativeAnnex'
        }
      }
      pageTypeMap[pageType] = item.sectionName

      item.expressions?.forEach((i) => {
        contentTypeMap[i.code] = i.name

        if (!ruleMap[i.code]) {
          ruleMap[i.code] = []
        }

        ruleMap[i.code].push(pageType)
      })
    })
    return { ruleMap, pageTypeMap, contentTypeMap }
  },
  ruleFonts(state) {
    return state.xmleditor.ruleFonts
  },
  editContentType(state) {
    return state.xmleditor.editorConfig.editContentType
  },
  filePropertyName(state) {
    return state.xmleditor.filePropertyName
  },
  collaborate(state) {
    return state.xmleditor.editorConfig.collaborate
  },
  entry(state) {
    return state.xmleditor.editorConfig.entry
  },
  _iframe(state) {
    return state.xmleditor.editorConfig._iframe
  },
  isChief(state) {
    return state.xmleditor.isChief
  },
  isTermDoc(state) {
    return state.xmleditor.editorConfig.editContentType === 'document' && state.xmleditor.filePropertyName === 'TERM'
  },
  isAbbrDoc(state) {
    return state.xmleditor.editorConfig.editContentType === 'document' && state.xmleditor.filePropertyName === 'ABBR'
  },
  languageType(state) {
    return state.xmleditor.languageType
  },
  docType(state) {
    return state.xmleditor.docType
  },
  isFormulaImage(state) {
    return state.xmleditor.isFormulaImage
  },
  signatureInfoList(state) {
    return state.xmleditor.signatureInfoList
  },
  rightSidebarItems(state) {
    if (state.xmleditor.editorConfig.editContentType === 'document') {
      return leftItems
    } else if (state.xmleditor.editorConfig.editContentType === 'template') {
      return leftItems.filter((i) => i.name === 'Outline')
    }
    return []
  },
  leftSidebarItems(state) {
    let left = [],
      right = []
    if (state.xmleditor.editorConfig.editContentType === 'document') {
      left = leftItems
    } else if (state.xmleditor.editorConfig.editContentType === 'template') {
      left = leftItems.filter((i) => i.name === 'Outline')
    }
    if (state.xmleditor.editorConfig.editContentType === 'document') {
      right = rightItems
    }
    // 在搜索结果、体系视图或文档列表中，编辑器的只读模式：预览、导出、搜索、收藏、查看关键词
    if (state.xmleditor.editorConfig.entry === 'preview' || state.xmleditor.editorConfig.entry === 'htmlpreview') {
      right = rightItems.filter((i) => i.name !== 'Comment')
    }
    return [...left, ...right]
  },
  mapDataIdAndIndex(state) {
    const obj = {}
    state.xmleditor.pageData
      .filter((item) => isAnnexByType(item.pageType) || item.pageType === 'introduction')
      .forEach((item) => {
        item?.blockList?.forEach((i) => {
          obj[i.blockId] = i.blockIndex
        })
      })
    state.xmleditor.pageData
      .filter((i) => allContentTypes.includes(i.dataType))
      .forEach((cur) => {
        function walk(item) {
          // blockId用于收藏块等插入元素
          obj[item.dataId || item.blockId] = item.dataIndex
          item?.children?.forEach((i) => {
            obj[i.dataId] = i.dataIndex
            walk(i)
          })
        }
        walk(cur)
      })
    return obj
  }
}
export default getters
